package util

import (
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
)

func GoroutineTry(ctx context.Context, do func(ctx context.Context), recovery func(ctx context.Context), tips string) {
	go func() {
		err := g.Try(ctx, func(ctx context.Context) {
			do(ctx)
		})
		if err != nil {
			stack := gerror.Stack(err)
			glog.Error(ctx, err, stack, "Goroutine Panic", tips)

			if recovery != nil {
				recovery(ctx)
			}
		}
	}()
}
